package univer.oko.database.rmi.impl;

import univer.oko.Error;

import java.util.Vector;

/**
 * @author Eduard Dudar
 *         Date: 19.11.2005
 *         Time: 1:11:05
 */
public class FacultyImpl extends ImplHelper implements DBMetadata {
    public FacultyImpl(RMIImpl rmi) {
        super(rmi);
    }

    public synchronized int addFaculty(String str) {
        if (this.isOccurs(SELECT + FACULTY_ID + FROM + FACULTY + WHERE + FACULTY_NAME + " = '" + str + "'")) {
            return Error.REPEAT;
        }

        int count = this.getIntValue(SELECT + MAX(FACULTY_ID) + FROM + FACULTY) + 1;
        this.executeUpdate(INSERT_INTO + FACULTY + " values(" + count + ",'" + str + "')");
        this.commit();
        return Error.ALRIGHT;
    }

    public synchronized Vector getFaculties() {
        return this.getVectorValue(SELECT + FACULTY_NAME + FROM + FACULTY + ORDER_BY + FACULTY_NAME);
    }

    public synchronized int deleteFaculty(String str) {
        if (this.isOccurs(
                SELECT + FACULTY_ID +
                        FROM + FACULTY + ", " + DIRECTION +
                        WHERE + DIRECTION_REF_FACULTY + " = " + FACULTY_ID + AND + FACULTY_NAME + " = '" + str + "'"))
        {
            return Error.IN_USE;
        }

        this.executeUpdate(DELETE_FROM + FACULTY + WHERE + FACULTY_NAME + " = '" + str + "'");
        this.commit();
        return Error.ALRIGHT;
    }

    public synchronized Integer getFacultyID(String str) {
        return this.getIntValue(SELECT + FACULTY_ID + FROM + FACULTY + WHERE + FACULTY_NAME + " = '" + str + "'");
    }

    public synchronized String getFacultyName(int id) {
        return this.getStringValue(SELECT + FACULTY_NAME + FROM + FACULTY + WHERE + FACULTY_ID + " = " + id);
    }

    public synchronized String getFacultyNameBySpeciality(String str) {
        return this.getStringValue(
                SELECT + FACULTY_NAME +
                        FROM + FACULTY + ", " + SPECIALITY +
                        WHERE + SPECIALITY_REF_FACULTY + " = " + FACULTY_ID + AND + SPECIALITY_NAME + " = '" + str + "'");
    }

    public synchronized String getFacultyNameByDirection(String str) {
        return this.getStringValue(
                SELECT + FACULTY_NAME +
                        FROM + FACULTY + ", " + DIRECTION +
                        WHERE + DIRECTION_REF_FACULTY + " = " + FACULTY_ID + AND + DIRECTION_NAME + " = '" + str + "'");
    }

    public synchronized String getFacultyNameByFinUnit(String str) {
        return this.getStringValue(
                SELECT + FACULTY_NAME +
                        FROM + FACULTY + ", [financial_item] " +
                        WHERE + " [financial_item].[ref_faculty] = " + FACULTY_ID + AND + " [financial_item].[finitem_name] = '" + str + "'");
    }
}
